/*
 * Project: PrEditor2
 * File:    ImageLoader.java
 * Created: Dec 15, 2004
 * By:      critchie
 * 
 * (C) 2004 Ontira Communications Inc.
 */
package com.googlecode.ddlutilstool.common;

import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

/**
 * <p>This object gets images and strings using the gui.properties file.
 * It was added to allow simpler changes to the gui, and for handling
 * multiple languages in the future.</p>
 * 
 * @author critchie
 */
public class GuiNameAccessor {

	private static final String IMAGE_FOLDER = "/META-INF/images/";
	private static final String RESOURCE_NAME = "gui";
	
	private static final Logger mLog = Logger.getLogger(GuiNameAccessor.class);
	
	private Toolkit mToolkit;
	private ResourceBundle mBundle;
	
	/**
	 * 
	 */
	public GuiNameAccessor() {
		super();
		mBundle = ResourceBundle.getBundle(RESOURCE_NAME,Locale.getDefault());
		mToolkit = Toolkit.getDefaultToolkit();
	}

	/**
	 * <p>Uses the given name to resolve an image file name in the
	 * gui resource bundle, and then loads the image file from
	 * the <pre>/META-INF/images/</pre> folder in the classpath.</p>
	 * 
	 * @param pName the property name pointing to the image file in the gui resource bundle
	 * @return The loaded image.
	 */
	public Image getImage(String pName) {
		String resolvedName = mBundle.getString(pName);
		URL imageURL = getClass().getResource(IMAGE_FOLDER+resolvedName);
	    try {
            imageURL.openConnection();
        } 
	    catch (Throwable e) {
	        throw new RuntimeException("Error opening image "+pName);
        }
	    finally {
	    }
		
		Image image = mToolkit.getImage(getClass().getResource(IMAGE_FOLDER+resolvedName));
		return image;
	}
	
	/**
	 * <p>Uses the given name to resolve an image file name in the
	 * gui resource bundle, and then loads the image file from
	 * the <pre>/META-INF/images/</pre> folder in the classpath.</p>
	 * 
	 * @param pName the property name pointing to the image file in the gui resource bundle
	 * @return The loaded image.
	 */
	public ImageIcon getImageIcon(String pName) {
		Image image = getImage(pName);
		return new ImageIcon(image);
	}
	
	/**
	 * <p>Resolves the given string using the gui resource bundle.</p>
	 * 
	 * @param pName the property name in the gui resource bundle
	 * @return the resolved string from the bundle
	 */
	public String getString(String pName) {
	    try {
	        String resolvedName = mBundle.getString(pName);
			return resolvedName;
	    }
	    catch (MissingResourceException e) {
	        if (mLog.isDebugEnabled()) mLog.debug(e.getMessage());
	        return pName;
	    }
	}


	/**
	 * <p>Resolves the given string using the gui resource bundle.
	 * It includes a single parameter, referred to as {0}.</p>
	 * 
	 * @param pName the property name in the gui resource bundle
	 * @param p1 the parameter
	 * @return the resolved string from the bundle
	 */
	public String getString(String pName, Object p1) {
		return getString(pName,new Object[]{p1});
	}
	
	
	/**
	 * <p>Resolves the given string using the gui resource bundle.
	 * It includes 2 parameters, referred to as {0} and {1}.</p>
	 * 
	 * @param pName the property name in the gui resource bundle
	 * @param p1 the first parameter
	 * @param p2 the second parameter
	 * @return the resolved string from the bundle
	 */
	public String getString(String pName, Object p1, Object p2) {
		return getString(pName,new Object[]{p1,p2});
	}
	
	/**
	 * <p>Resolves the given string using the gui resource bundle.
	 * It includes 3 parameters, referred to as {0} and {1} and {2}.</p>
	 * 
	 * @param pName the property name in the gui resource bundle
	 * @param p1 the first parameter
	 * @param p2 the second parameter
	 * @param p3 the third parameter
	 * @return the resolved string from the bundle
	 */
	public String getString(String pName, Object p1, Object p2, Object p3) {
		return getString(pName,new Object[]{p1,p2,p3});
	}
	
	/**
	 * <p>Resolves the given string using the gui resource bundle.
	 * It includes an array parameters, referred to as {0}, {1} and so forth.</p>
	 * 
	 * @param pName the property name in the gui resource bundle
	 * @param pArguments the array if parameters
	 * @return the resolved string from the bundle
	 */
	private String getString(String pName, Object[] pArguments) {
		String resolvedName = mBundle.getString(pName);
		MessageFormat formatter = new MessageFormat(resolvedName,Locale.getDefault());
		return formatter.format(pArguments);
	}
}
